package zqkcom.atguigu.servlet;

import zqkcom.atguigu.bean.Cart;
import zqkcom.atguigu.bean.Order;
import zqkcom.atguigu.bean.OrderItem;
import zqkcom.atguigu.bean.User;
import zqkcom.atguigu.service.OrderService;
import zqkcom.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class OrderClientServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();
    /**
     * 结账
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void checkOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //获取session中的购物车对象和当前登录的用户对象
        Cart cart = (Cart) session.getAttribute("cart");
        User user = (User) session.getAttribute("user");
        //调用service处理业务逻辑，实现结账的功能
        String orderId = orderService.checkOut(cart, user);
        //将购物车中的所有图书信息清空
        session.removeAttribute("cart");
        //将创建订单所生成的订单编号存储在session中
        session.setAttribute("orderId",orderId);
        //重定向到结账页面
        response.sendRedirect(request.getContextPath()+"/pages/cart/checkout.jsp");
    }

/**
 * 查询我的订单
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
protected void getMyOrder (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //获取session
    HttpSession session = request.getSession();
    //获取当前登录的用户信息
    User user = (User) session.getAttribute("user");
    //调用service处理业务逻辑，实现查询我的订单的功能
    List<Order> list = orderService.getMyOrder(user.getId());
    //将我的订单信息放在request请求域中进行共享
    request.setAttribute("list", list);
    //转发到我的订单页面
    request.getRequestDispatcher("/pages/client/order_client.jsp").forward(request, response);


}

    /**
     * 收货
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void takeOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，实现收货功能
        orderService.takeOrder(orderId);
        //重定向到来源页面
        response.sendRedirect(request.getContextPath() + "/OrderClientServlet?method=getMyOrder");
    }
    /**
     * 客户端的订单详情功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，获取订单详情
        List<OrderItem> list = orderService.getDetails(orderId);
        //将订单详情，即订单中所有订单项放在请求域中
        request.setAttribute("list",list);
        //转发到客户端的订单详情页面
        request.getRequestDispatcher("/pages/client/order_details.jsp").forward(request, response);
    }


}
